package com.china08.yxyapi.entity.es;

import java.io.IOException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

import com.china08.yxyapi.entity.ElasticBaseEntity;
import com.china08.yxyapi.entity.mg.res.AllTemplate;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

@Document(indexName = "yxy_yxy", type = "question")
public class ElasticYxyYxyQuesResource extends ElasticBaseEntity {

    private static final Logger LOGGER = LoggerFactory.getLogger(ElasticYxyYxyQuesResource.class);

    private static final long serialVersionUID = 1L;

    static JacksonXmlModule module = new JacksonXmlModule();
    static XmlMapper xmlMapper = null;

    static {
        module.setDefaultUseWrapper(true);
        xmlMapper = new XmlMapper(module);
    }

    /**
     * 难度级别 123
     */
    @Field
    private String difficultyLevel;

    /**
     * 难度系数0-1浮点
     */
    @Field
    private Float difficultyXishu;

    @Field
    private String abilityLevel;

    /**
     * 题关联的知识点
     */
    @Field
    private List<Integer> knowledgeIds;

    @Field
    private List<Integer> knowFullIds;

    /**
     * 题关联的知识点的名称
     */
    @Field
    private List<String> knowledgeTexts;

    /**
     * 题关联的考点
     */
    @Field
    private List<Integer> examIds;

    @Field
    private List<Integer> examFullIds;

    @Field
    private List<String> examTexts;

    @Field
    private String srcAreaId;

    @Field
    private String srcYear;

    @Field
    private String srcAb;

    @Field
    private String srcType;

    @Field
    private String srcName;

    // 1单选 2填空 8多选 其他解答
    @Field
    private String questionType;

    @Field
    private String questionScore;

    @Field
    private String body;

    @Field
    private String quesForSearch;

    @Field
    private String questionNo;

    @Field
    private String flag;

    @Field
    private List<String> oldIds;

    @Field
    private String docUrl;

    // COMMENT '1:删除，0：未删除
    @Field
    private String deleteFlag;

    @Field
    private String phase;

    @Field
    private String phaseName;

    @Field
    private String subject;

    @Field
    private String subjectName;

    @Field
    private String oid;

    @Field
    private Integer questionTime;

    // 查看范围 0：私有 1：学校 2： 教育云
    @Field
    private Integer viewScope = 2;

    // 作业系统使用次数
    @Field
    private Long homeworkUseCount;

    // 作业系统收藏次数
    @Field
    private Long homeworkFavoriteCount;

    // @JsonIgnore
    private Object quesBody;

    // @JsonIgnore
    private Object quesBody2;
    // getter setter

    public Object getQuesBody() throws JsonParseException, JsonMappingException, IOException {
        try {

            long startQueryBody = System.currentTimeMillis();

            System.out.println(" 试题xml 初始化 :  " + (System.currentTimeMillis() - startQueryBody));
            this.body = this.body.replace(",yxup", "https://yx-tiku-img.oss-cn-beijing.aliyuncs.com/yxup");
            this.body = this.body.replace("img.xueshentong.com", "yx-tiku-img.oss-cn-beijing.aliyuncs.com");
            this.body = this.body.replace("src=\"\\quiz",
                    "src=\"https://yx-tiku-img.oss-cn-beijing.aliyuncs.com\\quiz");
            this.body = this.body.replace("&nbsp;", " ");
            System.out.println(" 试题body replace  :  " + (System.currentTimeMillis() - startQueryBody));
            this.quesBody = xmlMapper.readValue(this.body, AllTemplate.class);
            System.out.println(" 试题 readValue()  :  " + (System.currentTimeMillis() - startQueryBody));
        } catch (Exception e) {
            LOGGER.error("ques(" + this.oldIds + ")-->quesBody 转换（template）异常!(tyw)", e);
        }
        return this.quesBody;
    }

    /**
     * 答案替换下划线
     * 
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public Object getQuesBody2() throws JsonParseException, JsonMappingException, IOException {
        try {

            long startQueryBody = System.currentTimeMillis();

            System.out.println(" 试题xml 初始化 :  " + (System.currentTimeMillis() - startQueryBody));
            this.body = this.body.replace(",yxup", "https://yx-tiku-img.oss-cn-beijing.aliyuncs.com/yxup");
            this.body = this.body.replace("img.xueshentong.com", "yx-tiku-img.oss-cn-beijing.aliyuncs.com");
            this.body = this.body.replace("src=\"\\quiz",
                    "src=\"https://yx-tiku-img.oss-cn-beijing.aliyuncs.com\\quiz");
            this.body = this.body.replace("&nbsp;", " ");
            System.out.println(" 试题body replace  :  " + (System.currentTimeMillis() - startQueryBody));
            this.quesBody = xmlMapper.readValue(this.body, AllTemplate.class);
            System.out.println(" 试题 readValue()  :  " + (System.currentTimeMillis() - startQueryBody));
            AllTemplate allTemplate = (AllTemplate) this.quesBody;
            org.jsoup.nodes.Document document = Jsoup.parse(allTemplate.sub.stem.body);
            org.jsoup.select.Elements elements = document.getElementsByClass("quizPutTag");
            for (Element element : elements) {
                element.html("________");
            }
            allTemplate.sub.stem.body = document.html();
            this.quesBody2 = allTemplate;
        } catch (Exception e) {
            LOGGER.error("ques(" + this.oldIds + ")-->quesBody 转换（template）异常!(tyw)", e);
        }
        return this.quesBody2;
    }

    public String getDifficultyLevel() {
        return difficultyLevel;
    }

    public void setDifficultyLevel(String difficultyLevel) {
        this.difficultyLevel = difficultyLevel;
    }

    public Float getDifficultyXishu() {
        return difficultyXishu;
    }

    public void setDifficultyXishu(Float difficultyXishu) {
        this.difficultyXishu = difficultyXishu;
    }

    public String getAbilityLevel() {
        return abilityLevel;
    }

    public void setAbilityLevel(String abilityLevel) {
        this.abilityLevel = abilityLevel;
    }

    public List<Integer> getKnowledgeIds() {
        return knowledgeIds;
    }

    public void setKnowledgeIds(List<Integer> knowledgeIds) {
        this.knowledgeIds = knowledgeIds;
    }

    public List<Integer> getKnowFullIds() {
        return knowFullIds;
    }

    public void setKnowFullIds(List<Integer> knowFullIds) {
        this.knowFullIds = knowFullIds;
    }

    public List<String> getKnowledgeTexts() {
        return knowledgeTexts;
    }

    public void setKnowledgeTexts(List<String> knowledgeTexts) {
        this.knowledgeTexts = knowledgeTexts;
    }

    public List<Integer> getExamIds() {
        return examIds;
    }

    public void setExamIds(List<Integer> examIds) {
        this.examIds = examIds;
    }

    public List<Integer> getExamFullIds() {
        return examFullIds;
    }

    public void setExamFullIds(List<Integer> examFullIds) {
        this.examFullIds = examFullIds;
    }

    public List<String> getExamTexts() {
        return examTexts;
    }

    public void setExamTexts(List<String> examTexts) {
        this.examTexts = examTexts;
    }

    public String getSrcAreaId() {
        return srcAreaId;
    }

    public void setSrcAreaId(String srcAreaId) {
        this.srcAreaId = srcAreaId;
    }

    public String getSrcYear() {
        return srcYear;
    }

    public void setSrcYear(String srcYear) {
        this.srcYear = srcYear;
    }

    public String getSrcAb() {
        return srcAb;
    }

    public void setSrcAb(String srcAb) {
        this.srcAb = srcAb;
    }

    public String getSrcType() {
        return srcType;
    }

    public void setSrcType(String srcType) {
        this.srcType = srcType;
    }

    public String getSrcName() {
        return srcName;
    }

    public void setSrcName(String srcName) {
        this.srcName = srcName;
    }

    public String getQuestionType() {
        return questionType;
    }

    public void setQuestionType(String questionType) {
        this.questionType = questionType;
    }

    public String getQuestionScore() {
        return questionScore;
    }

    public void setQuestionScore(String questionScore) {
        this.questionScore = questionScore;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getQuesForSearch() {
        return quesForSearch;
    }

    public void setQuesForSearch(String quesForSearch) {
        this.quesForSearch = quesForSearch;
    }

    public String getQuestionNo() {
        return questionNo;
    }

    public void setQuestionNo(String questionNo) {
        this.questionNo = questionNo;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public List<String> getOldIds() {
        return oldIds;
    }

    public void setOldIds(List<String> oldIds) {
        this.oldIds = oldIds;
    }

    public String getDocUrl() {
        return docUrl;
    }

    public void setDocUrl(String docUrl) {
        this.docUrl = docUrl;
    }

    public String getDeleteFlag() {
        return deleteFlag;
    }

    public void setDeleteFlag(String deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public String getPhase() {
        return phase;
    }

    public void setPhase(String phase) {
        this.phase = phase;
    }

    public String getPhaseName() {
        return phaseName;
    }

    public void setPhaseName(String phaseName) {
        this.phaseName = phaseName;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid;
    }

    public Integer getQuestionTime() {
        return questionTime;
    }

    public void setQuestionTime(Integer questionTime) {
        this.questionTime = questionTime;
    }

    public void setQuesBody(Object quesBody) {
        this.quesBody = quesBody;
    }

    public Integer getViewScope() {
        return viewScope;
    }

    public void setViewScope(Integer viewScope) {
        this.viewScope = viewScope;
    }

    public Long getHomeworkUseCount() {
        return homeworkUseCount;
    }

    public void setHomeworkUseCount(Long homeworkUseCount) {
        this.homeworkUseCount = homeworkUseCount;
    }

    public Long getHomeworkFavoriteCount() {
        return homeworkFavoriteCount;
    }

    public void setHomeworkFavoriteCount(Long homeworkFavoriteCount) {
        this.homeworkFavoriteCount = homeworkFavoriteCount;
    }

}
